/**********************************************************************************
// DemoAirSprite
// 
// Creation:	23 Apr 2011
// Updated:		27 May 2011
// Compiler:	Visual Studio 2010
//
// Notes:		Test the sprite capabilities of the engine
//				Draw, move, translate and rotate an airplane-like image
//
**********************************************************************************/

#define WIN32_LEAN_AND_MEAN  // just say no to MFC

#include "Resources.h"
#include "Anabele.h"	


/**********************************************************************************/
/*                           AirSprite Demo Class                                 */
/**********************************************************************************/


class AirSpriteDemo: public D3DGame
{
private:
	Input input;
	Sprite * plane;
	Sprite * demobox;
	Sprite * demobar;
	char text[256];

	float lastx, lasty;
	float scaling;
	float rotation;

public:
	void GameInit();
	void GameLogic();
	void GameDraw();
	void GameShutDown();
	void LoadResources();
	void UnloadResources();
};

/**********************************************************************************/

void AirSpriteDemo::GameInit()
{
	input.Initialize();
	input.InitializeKeyboard();

	lastx = GetWindowWidth()/2.0f;
	lasty = GetWindowHeight()/2.0f;
	scaling = 1.0f;
	rotation = 0.0f;

	LoadResources();
}

/**********************************************************************************/

void AirSpriteDemo::LoadResources()
{
	plane = new Sprite("Resources/airplane.png");
	plane->MoveTo(lastx, lasty);
	plane->scalingX = scaling;
	plane->scalingY = scaling;
	plane->rotation = rotation;

	demobox = new Sprite("Resources/demoBox.png");
	demobox->MoveTo(155, 100);
	
	demobar = new Sprite("Resources/demoBar.png");
	demobar->MoveTo(GetWindowWidth()/2.0f, GetWindowHeight()-30.0f);
}

/**********************************************************************************/

void AirSpriteDemo::UnloadResources()
{
	lastx = plane->x;
	lasty = plane->y;
	scaling = plane->scalingX;
	rotation = plane->rotation;	

	if (demobar) delete demobar;
	if (demobox) delete demobox;
	if (plane) delete plane;
}

/**********************************************************************************/

void AirSpriteDemo::GameShutDown()
{
	UnloadResources();
}

/**********************************************************************************/

void AirSpriteDemo::GameLogic()
{
	// for now test if user is hitting ESC and send WM_DESTROY
	EndsOnEscape();

	// get keyboard state change
	input.GetKeyboardState();

	// Scale
	if (input.KeyPressed(DIK_A)) // scale up
		plane->Scale(1.1f, 1.1f);
	else
		if (input.KeyPressed(DIK_S)) // scale down
		plane->Scale(0.9f, 0.9f);

	// Rotate
	if (input.KeyPressed(DIK_Z)) // rotate left
		plane->Rotate(-5);
	else
	if (input.KeyPressed(DIK_X)) // rotate right
		plane->Rotate(5);

	// Translate
	if (input.KeyPressed(DIK_RIGHT)) // translate right
		plane->Translate(5,0);
	else
	if (input.KeyPressed(DIK_LEFT))  // translate left
		plane->Translate(-5,0);    

	if (input.KeyPressed(DIK_UP))    // translate up
		plane->Translate(0,-5); 
	else
	if (input.KeyPressed(DIK_DOWN))  // translate down
		plane->Translate(0,5);
} 

/**********************************************************************************/

void AirSpriteDemo::GameDraw()
{
	plane->Draw();
	demobox->Draw();
	demobar->Draw();

	sprintf_s(text, "AirSprite Demo");
	sysFont->Draw(55, 40, text);

	sprintf_s(text, "Image loading, drawing and clipping");
	sysFont->Draw(55, 80, text);

	sprintf_s(text, "Translation, rotation and scaling");
	sysFont->Draw(55, 100, text);

	sprintf_s(text, "Image position: %d x %d", plane->IntX(), plane->IntY());
	sysFont->Draw(55, 120, text);

	sprintf_s(text, "<A> and <S> Scale    <Z> and <X> - Rotate    <Arrows> - Translate");
	sysFont->Draw(0, GetWindowHeight()-37.0f, text, Color(255,255,255), GetWindowWidth(), 30, DT_CENTER);
} 


/**********************************************************************************/
/*                                  WinMain                                       */
/**********************************************************************************/

int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
	AirSpriteDemo planeDemo;

	//planeDemo.SetWindowMode(WINDOWED);
	//planeDemo.SetWindowWidth(800);
	//planeDemo.SetWindowHeight(600);
	planeDemo.SetWindowBgColor(10,25,55);
	planeDemo.SetWindowTitle(string("AirSprite Demo"));
	planeDemo.SetWindowIcon(LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON)));
	planeDemo.SetWindowCursor(LoadCursor(hInstance, MAKEINTRESOURCE(IDI_CURSOR)));
	planeDemo.ShowWindowCursor(false);
	planeDemo.Initialize(hInstance);
	planeDemo.Start();
	
	return 0;

} 

/**********************************************************************************/

